
package podcastbyphone.client;

import twilio.client.Calls;
import twilio.client.Call;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.LazyPanel;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.google.gwt.user.client.ui.TabLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 * 
 */
public class PhoneApp implements EntryPoint
{

	/**
	 * Create a remote service proxy to talk to the server-side  service.
	 */
	private final RpcServiceAsync rpcService = GWT.create(RpcService.class);
	
	private final FlexTable podcastTable = new FlexTable();
	private final FlexTable callsTable = new FlexTable();
	private final VerticalPanel callsPanel = new VerticalPanel();

	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad()
	{
		
		DecoratedTabPanel tabPanel = new DecoratedTabPanel();
		tabPanel.add(this.podcastTable, "Podcasts");
		tabPanel.add(this.callsPanel, "Calls");
		tabPanel.selectTab(0);
		
		DockLayoutPanel layoutPanel = new DockLayoutPanel(Unit.EM);
		layoutPanel.add(tabPanel);
		
		
		Button refreshButton = new Button("Refresh");
		
		refreshButton.addClickHandler(new ClickHandler()
		{
			public void onClick(ClickEvent event)
			{
				refreshAll();
			}
		});
		
		callsPanel.add(refreshButton);
		callsPanel.add(callsTable);

		podcastTable.setVisible(true);
		callsTable.setVisible(true);
		
		refreshAll();
		
		RootLayoutPanel.get().add(layoutPanel);

	}
	
	private void refreshAll()
	{
		refreshPodcastTable();
		refreshCallsTable();
	}

	protected void refreshPodcastTable()
	{
		rpcService.getPodcastMap(new AsyncCallback<PodcastMap>()
				{

					public void onFailure(Throwable caught)
					{
						podcastTable.removeAllRows();
						podcastTable.setText(0, 0, "failure");
						if (caught != null)
						{
							podcastTable.setText(1, 0, caught.getClass().getName());
						}
						
					}

					public void onSuccess(PodcastMap pmap)
					{
						podcastTable.removeAllRows();
						
						for (Integer digit : pmap.keySet())
						{
							int row = digit.intValue();
							Podcast p = pmap.get(digit);
							podcastTable.setText(row, 0, "" + digit);
							podcastTable.setText(row, 1, p.getDescription());
						}
						
						if ( !podcastTable.isVisible() )
						{
							podcastTable.setVisible(true);
						}
						
						
					}
				});
	}

	
	protected void refreshCallsTable()
	{
		rpcService.getCalls(30, new AsyncCallback<twilio.client.Calls>()
				{

					public void onFailure(Throwable caught)
					{
						callsTable.removeAllRows();
						callsTable.setText(0, 0, "failure");
						if (caught != null)
						{
							callsTable.setText(1, 0, caught.getClass().getName());
						}
						
					}

					public void onSuccess(Calls calls)
					{
						
						System.out.println("calls.size() = " + calls.size());
						callsTable.removeAllRows();
						
						callsTable.setText(0, 0, "Start Time");
						callsTable.setText(0, 1, "End Time");
						callsTable.setText(0, 2, "Caller");
						callsTable.setText(0, 3, "Called");
						callsTable.setText(0, 4, "Status");
						callsTable.setText(0, 5, "Inbound?");
						
						int row = 1;
						
						for (Call c : calls)
						{
							String startTime = "";
							if (c.getStartTime() != null)
							{
								startTime = c.getStartTime().toString();
							}
							
							String endTime = "";
							if (c.getEndTime() != null)
							{
								endTime = c.getEndTime().toString();
							}
							
							callsTable.setText(row, 0, startTime);
							callsTable.setText(row, 1, endTime);
							callsTable.setText(row, 2, "nnn-nnn-" + c.getCaller().substring(c.getCaller().length() - 4));
							callsTable.setText(row, 3, "nnn-nnn-" + c.getCalled().substring(c.getCalled().length() - 4));
							callsTable.setText(row, 4, c.getCallStatus().name().toUpperCase());
							callsTable.setText(row, 5, c.wasInbound() ? "Inbound" : "Outbound");
							
							row++;
						}
						
						
						if ( !callsTable.isVisible() )
						{
							callsTable.setVisible(true);
						}
						
						
					}
				});
	}


}
